/**
 * Project: apollo-base-tools
 * 
 * File Created at 2017年8月1日
 * 
 * Copyright 2015-2016 dx.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * DongXue software Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with dx.com.
 */
package org.apollo.base.tools.ui.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.IOException;
import java.util.Properties;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;

/** 
* @ClassName: IconUtil 
* @Description: 图片工具
* @author wuzhenfang(wzfbj2008@163.com)
* @date 2017年8月1日 下午9:07:23 
* @version V1.0 
*/
public class IconUtil {

	public static Color DEFAULT_TEXT_COLOR = new Color(37, 81, 54);
	public static Font FONT_14_BOLD = new Font("Calibri", 1, 14);
	public static Font FONT_BOLD = new Font("Calibri", 1, 13);
	public static Font FONT_PLAIN = new Font("Calibri", 0, 14);
	public static Font FONT_13_PLAIN = new Font("Calibri", 0, 13);
	public static Color MENUITEM_SELECTED_BACKGROUND = new Color(166, 188, 140);
	public static Color MENUITEM_BACKGROUND = new Color(228, 235, 218);
	public static Color CONTENT_PANE_BACKGROUND = new Color(92, 153, 45);
	public static Color BUTTON_ROVER_COLOR = new Color(196, 196, 197);
	public static Color OUTLOOK_TEXT_COLOR = new Color(120, 120, 125);
	public static Color OUTLOOK_SPLIT_COLOR = new Color(174, 171, 162);
	public static Color OUTLOOK_CONTAINER_COLOR = new Color(217, 218, 219);
	public static Color TAB_BOTTOM_LINE_COLOR = new Color(167, 173, 175);
	public static Color LIST_SPLIT_COLOR = new Color(105, 113, 120);
	public static Color LIST_BACKGROUND = new Color(175, 174, 176);
	public static Color LIST_TEXT_COLOR = new Color(49, 52, 58);
	public static Color NO_COLOR = new Color(0, 0, 0, 0);
	public static Insets ZERO_INSETS = new Insets(0, 0, 0, 0);

	public static Image getImage(String image) {
		BufferedImage img = null;
		try {
			img = ImageIO.read(ClassLoader.getSystemResource(image));
		} catch (IOException e) {
			System.out.println("图片路径找不到: " + image);
		}
		return img;
	}

	public static Image getImage(String image, int imageNo) {
		BufferedImage img = null;
		try {
			img = ImageIO.read(ClassLoader.getSystemResource(image));
		} catch (IOException e) {
			System.out.println("图片路径找不到: " + image);
		}
		return img;
	}

	public static BufferedImage getGrayPicture(BufferedImage originalPic) {
		int imageWidth = originalPic.getWidth();
		int imageHeight = originalPic.getHeight();
		BufferedImage newPic = new BufferedImage(imageWidth, imageHeight, 10);

		ColorConvertOp cco = new ColorConvertOp(ColorSpace.getInstance(1003), null);

		cco.filter(originalPic, newPic);
		return newPic;
	}

	public static ImageIcon getImageIcon(String imgFile) {
		ImageIcon icon = null;
		try {
			return new ImageIcon(IconUtil.class.getResource(imgFile));
		} catch (Exception e) {
			System.out.println("找到不到图片文件 " + imgFile);
		}
		return icon;
	}

	public static Properties getSkinFile() {
		Properties prop = new Properties();
		try {
			prop.load(IconUtil.class.getResourceAsStream("blue-skin.properties"));
		} catch (IOException ex) {
			// Logger.getLogger(SkinUtil.class.getName()).log(Level.SEVERE, null, ex);
		}
		return prop;
	}

	public static ImageIcon getImageIcon(String imgFile, int x, int y, int x2, int y2) {
		if ((imgFile == null) || (imgFile.trim().length() == 0)) {
			return null;
		}
		ImageIcon icon = null;
		try {
			Image img = createBufferedImage(getImage(imgFile), x, y, x2, y2);
			if (img != null) {
			}
			return new ImageIcon(img);
		} catch (Exception e) {
			System.out.println("找不到图片 " + imgFile);
		}
		return icon;
	}

	public static ImageIcon getImageIcon(String imgFile, boolean grap) {
		if ((imgFile == null) || (imgFile.trim().length() == 0)) {
			return null;
		}
		ImageIcon icon = null;
		try {
			Image img = null;
			if (grap) {
				img = createBufferedImage(getImage(imgFile));
			} else {
				img = getGrayPicture(createBufferedImage(getImage(imgFile)));
			}
			if (img != null) {
			}
			return new ImageIcon(img);
		} catch (Exception e) {
			System.out.println("找不到图片文件" + imgFile);
		}
		return icon;
	}

	public static BufferedImage getBufferedImage(String name, JComponent component) {
		if ((name == null) || (name.trim().length() == 0)) {
			return null;
		}
		if (name.indexOf(",") != -1) {
			String[] str = name.split(",");
			String imgFile = str[0];
			Image image = getImage(imgFile);
			if (image == null) {
				return null;
			}
			return createBufferedImage(image, toInt(str[1]), toInt(str[2]), toInt(str[3]), toInt(str[4]));
		}
		Image image = getImage(name);
		if (image == null) {
			return null;
		}
		return createBufferedImage(image);
	}

	public static void setFont(String name, JComponent component) {
		if ((name == null) || (name.trim().length() == 0)) {
			return;
		}
		String[] value = name.split(",");
		Font font = null;
		try {
			if (value.length == 3) {
				font = new Font(value[0], Integer.parseInt(value[1]), Integer.parseInt(value[2]));
			} else {
				font = component.getFont().deriveFont(component.getFont().getSize() + 2);
			}
			if (font != null) {
				component.setFont(font);
			}
		} catch (Exception e) {
		}
	}

	public static void setColor(String name, JComponent component, String back) {
		if ((name == null) || (name.trim().length() == 0)) {
			return;
		}
		String[] value = name.split(",");
		Color color = null;
		try {
			color = new Color(Integer.parseInt(value[0]), Integer.parseInt(value[1]), Integer.parseInt(value[2]), Integer.parseInt(value[3]));
			if ((color != null) && ("back".equals(back))) {
				component.setBackground(color);
			} else if ((color != null) && ("fore".equals(back))) {
				component.setForeground(color);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void setBorder(String name, JComponent component) {
		if ((name == null) || (name.trim().length() == 0)) {
			return;
		}
		String[] value = name.split(",");
		try {
			if (value.length > 3) {
				component.setBorder(BorderFactory.createEmptyBorder(toInt(value[0]), toInt(value[1]), toInt(value[2]), toInt(value[3])));
			}
		} catch (Exception e) {
		}
	}

	public static int getWidth(String name, JComponent component) {
		if ((name == null) || (name.trim().length() == 0)) {
			return -1;
		}
		if (name.equalsIgnoreCase("X")) {
			return -1;
		}
		return Integer.parseInt(name);
	}

	public static int getHeight(String name, JComponent component) {
		if ((name == null) || (name.trim().length() == 0)) {
			return -1;
		}
		if (name.equalsIgnoreCase("X")) {
			return -1;
		}
		return Integer.parseInt(name);
	}

	public static int toInt(String value) {
		if ((value == null) || (value.trim().length() == 0)) {
			return 0;
		}
		try {
			return Integer.parseInt(value);
		} catch (Exception e) {
		}
		return 0;
	}

	public static void drawImageWith9Grids(Graphics paramGraphics, Image paramImage, int paramInt1, int paramInt2, int paramInt3, int paramInt4, Insets paramInsets,
			boolean paramBoolean) {
		int i = paramImage.getWidth(null);
		int j = paramImage.getHeight(null);
		paramGraphics.drawImage(paramImage, paramInt1, paramInt2, paramInt1 + paramInsets.left, paramInt2 + paramInsets.top, 0, 0, paramInsets.left, paramInsets.top, null);
		paramGraphics.drawImage(paramImage, paramInt3 - paramInsets.right, paramInt2, paramInt3, paramInt2 + paramInsets.top, i - paramInsets.right, 0, i, paramInsets.top, null);
		paramGraphics.drawImage(paramImage, paramInt1, paramInt4 - paramInsets.bottom, paramInt1 + paramInsets.left, paramInt4, 0, j - paramInsets.bottom, paramInsets.left, j,
				null);
		paramGraphics.drawImage(paramImage, paramInt3 - paramInsets.right, paramInt4 - paramInsets.bottom, paramInt3, paramInt4, i - paramInsets.right, j - paramInsets.bottom, i,
				j, null);
		paramGraphics.drawImage(paramImage, paramInt1 + paramInsets.left, paramInt2, paramInt3 - paramInsets.right, paramInt2 + paramInsets.top, paramInsets.left, 0,
				i - paramInsets.right, paramInsets.top, null);
		paramGraphics.drawImage(paramImage, paramInt1 + paramInsets.left, paramInt4 - paramInsets.bottom, paramInt3 - paramInsets.right, paramInt4, paramInsets.left,
				j - paramInsets.bottom, i - paramInsets.right, j, null);
		paramGraphics.drawImage(paramImage, paramInt1, paramInt2 + paramInsets.top, paramInt1 + paramInsets.left, paramInt4 - paramInsets.bottom, 0, paramInsets.top,
				paramInsets.left, j - paramInsets.bottom, null);
		paramGraphics.drawImage(paramImage, paramInt3 - paramInsets.right, paramInt2 + paramInsets.top, paramInt3, paramInt4 - paramInsets.bottom, i - paramInsets.right,
				paramInsets.top, i, j - paramInsets.bottom, null);
		if (!paramBoolean) {
			return;
		}
		paramGraphics.drawImage(paramImage, paramInt1 + paramInsets.left, paramInt2 + paramInsets.top, paramInt3 - paramInsets.right, paramInt4 - paramInsets.bottom,
				paramInsets.left, paramInsets.top, i - paramInsets.right, j - paramInsets.bottom, null);
	}

	public static BufferedImage createBufferedImage(Image img) {
		BufferedImage buff = new BufferedImage(img.getWidth(null), img.getHeight(null), 2);
		Graphics gfx = buff.createGraphics();
		gfx.drawImage(img, 0, 0, null);
		gfx.dispose();
		return buff;
	}

	public static BufferedImage createBufferedImage(Image img, int x, int y, int x2, int y2) {
		BufferedImage buff = new BufferedImage(x2 - x, y2 - y, 2);

		Graphics gfx = buff.createGraphics();
		gfx.drawImage(img, 0, 0, x2 - x, y2 - y, x, y, x2, y2, null);
		return buff;
	}
}
